package com.pc.web.cart;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pc.web.goods.GoodsBeans;

@Service
public class CartService {
	@Autowired
	private CartDao dao;

	// 상품정보 입력
	public int insert(CartBeans beans, HttpServletRequest request) {
		int sucs = 0;
		HttpSession session = request.getSession(true);
		String id = (String)session.getAttribute("user");
		beans.setCart_Id(id);
		System.out.println("아이디 : "+id);
		System.out.println("상품번호 : "+ request.getParameter("goods_code"));
		beans.setCart_GoodsNum(Integer.parseInt(request.getParameter("goods_code")));
		GoodsBeans insert = dao.getGoodsInfo(beans);
		beans.setCart_GoodsImage(insert.getGoods_image1());
		beans.setCart_GoodsImage2(insert.getGoods_image3());
		beans.setCart_GoodsName(insert.getGoods_name());
		beans.setCart_GoodsOption("나중에 추가");
		beans.setCart_GoodsPrice(insert.getGoods_cost());
		beans.setCart_GoodsAmount(Integer.parseInt(request.getParameter("amount")));
		beans.setCart_GoodsCount(insert.getGoods_count());
		sucs += dao.insert(beans);
		return sucs;
	}
	// 리스트 생성
	public List<CartBeans> list(CartBeans beans, HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		String id = (String)session.getAttribute("user");
		beans.setCart_Id(id);
		System.out.println("리스트아이디 :"+id);
		return dao.List(beans);
	}
	//상품 수정
	public int update(CartBeans beans) {
		int sucs = 0;
		for (int i=0; i<beans.getCart_Nums().length; i++)
		{
			beans.setCart_Num(beans.getCart_Nums()[i]);
			beans.setCart_GoodsAmount(beans.getCart_GoodsAmounts()[i]);
			beans.setCart_Sum(beans.getCart_Sums()[i]);
			sucs += dao.update(beans);
		}
		return sucs;
	}
	// 상품 삭제
	public int delete(CartBeans beans) {
		int sucs = 0;
		for (int i=0; i<beans.getCart_Nums().length; i++)
		{
			beans.setCart_Num(beans.getCart_Nums()[i]);
			sucs += dao.delete(beans);	
		}
		return sucs;
	}
}
